//
//  XMLHelper.m
//  PokerReplayer
//
//  Created by 徐 庆 on 11-4-2.
//  Copyright 2011 远航游戏中心. All rights reserved.
//

#import <Foundation/Foundation.h>
//#include <libxml/xmlmemory.h>
#import "XMLHelper.h"
//#import "XPathQuery.h"
#import "GDataXMLNode.h"


@implementation XMLHelper
@synthesize currentElenment;
@synthesize xmlNodes;

#pragma mark test use XMLparser 
/*
-(void)test{
	//找到本地test.xml文件  
	NSString * path = [[NSBundle mainBundle] pathForResource:@"test"ofType:@"xml"];
	NSFileHandle * file = [NSFileHandle fileHandleForReadingAtPath:path];
	NSData * data = [file readDataToEndOfFile];//得到xml文件  
	[file closeFile];
	
	//开始解析  
	NSXMLParser * xmlRead = [[NSXMLParser alloc] initWithData:data];//初始化NSXMLParser对象  
	[data release];  
	[xmlRead setDelegate:self];                                     //设置NSXMLParser对象的@解析方法代理  
	[xmlRead parse];                                                //调用代理解析NSXMLParser对象，看解析是
	
}

-(void)parserDidStartDocument:(NSXMLParser *)parser{
	Node = [[XMLNode alloc] init];
}


//节点起始。这里做节点属性的解析
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
	//这里的currentElement是将要处理的节点保存下来，下面的foundCharacters方法不知道他在处理那个节点
	currentElenment = elementName;
	
}

//这里来解析xml数据 xmlparser解析很麻烦，可以用libxml2来解析成xmldom会方便很多，貌似没有层次解构相同节点名称怎么解析？
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	if ([currentElenment isEqual:@"Player_Info"]) {
		
	}
	if ([currentElenment isEqual:@"PlayerCount"]) {
		Node.m_playerInfoNode.playerCount = string;
		NSLog(@"value: %@    parser:%@", Node.m_playerInfoNode.playerCount, parser);
	}
	if ([currentElenment isEqual:@"FirstCallLordCard"]){
		Node.m_playerInfoNode.firstCallLordCard = string;
	}
}

//节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	//currentElenment = @"TagToEnd";
}
*/

- (void)test{
	//初始化xmlNodes节点
	xmlNodes = [[XMLNodes alloc] init];
	
	// load xml file as text
	NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
	NSString *fileText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
	//NSLog(@"path:%d , fileText:%d", [path retainCount], [fileText retainCount]);
	
	// parse text as xml
	NSError *error;
	GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:fileText options:0 error:&error];
	GDataXMLElement *rootNode = [document rootElement];
	NSArray *playerCount = [rootNode nodesForXPath:@"//疯狂斗地主/Player_Info/PlayerCount" error:&error];
	self.xmlNodes.m_playerInfoNode.playerCount = [[playerCount objectAtIndex:0] stringValue];
	
	NSArray *FirstCallLordCard = [rootNode nodesForXPath:@"//疯狂斗地主/Player_Info/FirstCallLordCard" error:&error];
	self.xmlNodes.m_playerInfoNode.firstCallLordCard = [[FirstCallLordCard objectAtIndex:0] stringValue];
	
	// set playerinfo
	for (int i=0; i<[self.xmlNodes.m_playerInfoNode.playerCount intValue]; i++) {
		NSString *nodePath = [NSString stringWithFormat:@"%@%d",@"//疯狂斗地主/Player_Info/Player", i];
		NSArray *playerBaseInfo = [rootNode nodesForXPath:nodePath error:&error];
		PlayerInfo *playerElementInfo = [[PlayerInfo alloc] init];
		playerElementInfo.userName = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"Name"] stringValue];
		playerElementInfo.ID = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"ID"] stringValue];
		playerElementInfo.sex = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"Sex"] stringValue];
		[self.xmlNodes.m_playerInfoNode.m_playerInfo addObject:playerElementInfo];
		
		//[playerElementInfo release];
		//[playerBaseInfo release];
		[nodePath release];
	}
	
	
	NSLog(@"test");
}

#pragma mark 解析xml数据,采用google的库来解析
- (id)parserXML{
	//初始化xmlNodes节点
	xmlNodes = [[XMLNodes alloc] init];
	
	// load xml file as text
	NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
	NSString *fileText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
	//NSLog(@"path:%d , fileText:%d", [path retainCount], [fileText retainCount]);
	
	// parse text as xml
	NSError *error;
	GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:fileText options:0 error:&error];
	GDataXMLElement *rootNode = [document rootElement];
	//NSLog(rootNode.XMLString);  整个xml的string
	
	/*玩家信息节点的解析start*/
	// set playerCount
	//NSArray *playerCount = [rootNode elementsForName:@"Player_Info"]; 这里是另外一种查找节点信息的的方式只能从根节点往下找一个节点
	NSArray *playerCount = [rootNode nodesForXPath:@"//疯狂斗地主/Player_Info/PlayerCount" error:&error];
	self.xmlNodes.m_playerInfoNode.playerCount = [[playerCount objectAtIndex:0] stringValue];	
	
	// set FirstCallLordCard
	NSArray *FirstCallLordCard = [rootNode nodesForXPath:@"//疯狂斗地主/Player_Info/FirstCallLordCard" error:&error];
	self.xmlNodes.m_playerInfoNode.firstCallLordCard = [[FirstCallLordCard objectAtIndex:0] stringValue];
	
	// set playerinfo
	for (int i=0; i<[self.xmlNodes.m_playerInfoNode.playerCount intValue]; i++) {
		//NSLog(@"Name: %@",[[[playerBaseInfo objectAtIndex:0] attributeForName:@"Name"] stringValue]);
		NSString *nodePath = [NSString stringWithFormat:@"%@%d",@"//疯狂斗地主/Player_Info/Player", i];
		//NSString * nodePath = [@"//疯狂斗地主/Player_Info/Player" stringByAppendingFormat:[NSString stringWithFormat:@"%d", i]];  这里会出项一个格式话类型的警告有空再研究
		NSArray *playerBaseInfo = [rootNode nodesForXPath:nodePath error:&error];
		PlayerInfo *playerElementInfo = [[PlayerInfo alloc] init];
		playerElementInfo.userName = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"Name"] stringValue];
		playerElementInfo.ID = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"ID"] stringValue];
		playerElementInfo.sex = [[[playerBaseInfo objectAtIndex:0] attributeForName:@"Sex"] stringValue];
		[self.xmlNodes.m_playerInfoNode.m_playerInfo addObject:playerElementInfo];
		//NSLog(@"playerBaseInfo: %@", [[self.xmlNodes.m_playerInfoNode.m_playerInfo objectAtIndex:i] userName]);
		
		[playerElementInfo release];
		//[playerBaseInfo release];
	}
	/*玩家信息节点的解析end*/
#pragma mark ----------------------------------------------
	
	/*游戏信息节点解析start*/
	
	/*动作节点解析start*/
	NSArray *actionList = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/Action" error:&error];
	for ( GDataXMLNode *actionNodes in actionList) {
		ActionNode *xmlActionNode = [[ActionNode alloc] init];
		
		// set handCard
		GDataXMLNode *playerHandCard =  (GDataXMLNode *)[[actionNodes nodesForXPath:@"HandCard" error:&error] objectAtIndex:0];
		for (int i=0; i<[self.xmlNodes.m_playerInfoNode.playerCount intValue]; i++) {
			NSString * nodePath = [NSString stringWithFormat:@"%@%d", @"Player", i];
			PokerList *poke = [[PokerList alloc] init];
			NSArray * pokeList = [playerHandCard nodesForXPath:nodePath error:&error];
			//NSString * str = [[pokeList objectAtIndex:0] stringValue];
			poke.pokers = [[[pokeList objectAtIndex:0] stringValue] componentsSeparatedByString:@";"];
			//NSLog(@"poke: %@", poke);
			[xmlActionNode.playerHandCard.playerHandCardList addObject:poke];
			//[self.xmlNodes.m_gameInfoNode.gameActionNode.playerHandCard.playerHandCardList addObject:poke];
			//NSLog(@"handCard: %@", self.xmlNodes.m_gameInfoNode.gameActionNode.playerHandCard);
		}
		
		// set outedCard
		GDataXMLNode *playerOutedCard =  (GDataXMLNode *)[[actionNodes nodesForXPath:@"OutedCard" error:&error] objectAtIndex:0];
		for (int i=0; i<[self.xmlNodes.m_playerInfoNode.playerCount intValue]; i++) {
			NSString * nodePath = [NSString stringWithFormat:@"%@%d", @"Player", i];
			PokerList *poke = [[PokerList alloc] init];
			NSArray * pokeList = [playerOutedCard nodesForXPath:nodePath error:&error];
			poke.pokers = [[[pokeList objectAtIndex:0] stringValue] componentsSeparatedByString:@";"];
			[xmlActionNode.playerOutedCard.playerOutedCardList addObject:poke];
		}
		
		// set hideCard
		NSArray *playerHideCard =  [actionNodes nodesForXPath:@"HideCard/Cards" error:&error];
		PokerList *poke = [[PokerList alloc] init];
		poke.pokers = [[[playerHideCard objectAtIndex:0] stringValue] componentsSeparatedByString:@";"];
		xmlActionNode.playerHideCard.actionHideCard = poke.pokers;
		
		// set laizi
		NSArray *laiZi = [actionNodes nodesForXPath:@"NaiZi" error:&error];
		xmlActionNode.laizi = [[laiZi objectAtIndex:0] stringValue];
		
		// set GameTimes
		NSArray *gameTimes = [actionNodes nodesForXPath:@"GameTimes" error:&error];
		xmlActionNode.gameTimes = [[gameTimes objectAtIndex:0] stringValue];
		
		// set PlayerState
		GDataXMLNode *playerStateList =  (GDataXMLNode *)[[actionNodes nodesForXPath:@"PlayerState" error:&error] objectAtIndex:0];
		for (int i=0; i<[self.xmlNodes.m_playerInfoNode.playerCount intValue]; i++) {
			NSString * nodePath = [NSString stringWithFormat:@"%@%d", @"Player", i];
			NSArray * playerState = [playerStateList nodesForXPath:nodePath error:&error];
			
			PlayerStateInfo *playerStateInfo = [[PlayerStateInfo alloc] init];
			playerStateInfo.identity_state = [[[playerState objectAtIndex:0] attributeForName:@"Identity_state"] stringValue];
			playerStateInfo.action_state = [[[playerState objectAtIndex:0] attributeForName:@"Action_state"] stringValue];
			playerStateInfo.mingCard_state = [[[playerState objectAtIndex:0] attributeForName:@"MingCard_state"] stringValue];
			[xmlActionNode.playerState.playerStateList count];
			[xmlActionNode.playerState.playerStateList addObject:playerStateInfo];
			
			
			[playerStateInfo release];
		}
		
		[self.xmlNodes.m_gameInfoNode.gameActionNode addObject:xmlActionNode];

	}
	/*动作节点解析end*/
	#pragma mark ----------------------------------------------
	
	/*游戏结束信息节点解析start*/
	// set changePoint
	NSArray *changePoint = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/ChangePoint" error:&error]; 
	self.xmlNodes.m_gameInfoNode.gameOverInfo.changePoint = [[changePoint objectAtIndex:0] stringValue];
	
	// set Winner
	NSArray *winner = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/Winner" error:&error]; 
	self.xmlNodes.m_gameInfoNode.gameOverInfo.winner = [[winner objectAtIndex:0] stringValue];
	
	// set GameSumTimes
	NSArray *gameSumTimes = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/GameTimesSum/GameSumTimes" error:&error];
	self.xmlNodes.m_gameInfoNode.gameOverInfo.gameSumTimes = [[gameSumTimes objectAtIndex:0] stringValue];
	
	// set GrabLordTimes
	NSArray *grabLordTimes = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/GameTimesSum/GrabLordTimes" error:&error];
	self.xmlNodes.m_gameInfoNode.gameOverInfo.grabLordTimes = [[grabLordTimes objectAtIndex:0] stringValue];
	
	// set BombTimes
	NSArray *bombTimes = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/GameTimesSum/BombTimes" error:&error];
	self.xmlNodes.m_gameInfoNode.gameOverInfo.bombTimes = [[bombTimes objectAtIndex:0] stringValue];
	
	// set MingCard
	NSArray *mingCard = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/GameTimesSum/MingCard" error:&error];
	self.xmlNodes.m_gameInfoNode.gameOverInfo.mingCard = [[mingCard objectAtIndex:0] stringValue];
	
	// set MingCardBegin
	NSArray *mingCardBegin = [rootNode nodesForXPath:@"//疯狂斗地主/GameInfo/GameOver/GameTimesSum/MingCardBegin" error:&error];
	self.xmlNodes.m_gameInfoNode.gameOverInfo.mingCardBegin = [[mingCardBegin objectAtIndex:0] stringValue];
	/*游戏结束信息节点解析end*/
	
	/*游戏信息节点解析end*/
#pragma mark ----------------------------------------------
	
	//回收资源
//	[changePoint release];
//	[winner release];
//	[gameSumTimes release];
//	[grabLordTimes release];
//	[bombTimes release];
//	[mingCard release];
//	[mingCardBegin release];
	[document release];
	
	return xmlNodes;
}

@end
